package airthmetic.exercise.stack;

import java.util.Stack;

public class _20_有效的括号 {

    public static boolean isValid(String s) {
        if(s.isEmpty()){
            return true;
        }
        char[] chars = s.toCharArray();
        if(chars.length % 2 != 0){
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i] == '('){
                stack.push(')');
            }else if(chars[i] == '{'){
                stack.push('}');
            }else if(chars[i] == '['){
                stack.push(']');
            }else if(stack.isEmpty() || stack.pop() != chars[i]){
                return false;
            }
        }

        return stack.isEmpty();


    }

    public static void main(String[] args) {
        String s = "{}[()]";
        System.out.println(isValid(s));

        String s2 = "{}[()]}";
        System.out.println(isValid(s2));

        String s3 = "{{}[()]";
        System.out.println(isValid(s3));
    }
}
